import {Base} from "./base.js";
class Address extends Base{
constructor(){
   super();
}
//设置address地址
setAddressInfo(res){
   var province = res.provinceName || res.province,
       city = res.cityName || res.city,
       county = res.countyName || res.country,
       detail = res.detailInfo || res.detail;
   //拼接后的地址
   var finalAddress;
   var isCenterCitys = this.isCenterCity(province);
   if(isCenterCitys){
      finalAddress = city + county + detail;
   }else{
      finalAddress = province + city + county + detail;
   }
   return finalAddress;
}
//判断是不是直辖市
isCenterCity(name){
   var centerCity = ['北京市','天津市','上海市','重庆市'];
   if(centerCity.indexOf(name) >= 0){
      return true;
   }
}
//把提交的地址改为和数据库一致的字段
_setUpAddress(res){
   let formData = {
      name: res.userName,
      province: res.provinceName,
      city: res.cityName,
      country: res.countyName,
      mobile: '15606508983',
      detail: res.detailInfo
   }
   return formData;
}
//把地址发送到达服务器保存起来
submitAddress(data, callback){
   var data = this._setUpAddress(data); 
   var param = {
      url: 'address',
      type: 'post',
      data: data,
      scallback:function(res){
         callback && callback(res);
      },
      ecallback:function(res){
         callback && callback(res);
      }
   }
   this.request(param);
}
//从服务器获取地址
getAddress(callback){
   let that = this;
   let params = {
      url: 'getAddress',
      scallback:function(res){
         if(res){
            res.totalDetail = that.setAddressInfo(res);
            callback && callback(res);
         }
      }
   }
   this.request(params);
}
}
export {Address};